﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.IO;

namespace HA.ModFlow.Domain.FileLayer
{
   public class ModFlowFiles
    {
       public ModFlowFiles()
       {
           _basicPackage = new PBasic();
           _packages = new List<Package>();
           _discretizationPackage = new PDiscretization();
 
       }



        PBasic _basicPackage;

        public PBasic BasicPackage
        {
            get { return _basicPackage; }
            set { _basicPackage = value; }
        }


        PDiscretization _discretizationPackage;

        public PDiscretization DiscretizationPackage
        {
            get { return _discretizationPackage; }
            set { _discretizationPackage = value; }
        }


        List<Package> _packages;

        public List<Package> Packages
        {
            get { return _packages; }
            set { _packages = value; }
        }
       /// <summary>
       /// 写 *.Nam文件
       /// </summary>
       /// <param name="FileName">路径加名称</param>
        public void WriteNamFile(string FileNamePath)
        {
            string content = "";

            content += "LIST 80 solution.lst \r\n";


            content += this.BasicPackage.FileTypeString + "  " + this.BasicPackage.Nunit + "  " + this.BasicPackage.Fname + "\r\n";
            content += this.DiscretizationPackage.FileTypeString + "  " + this.DiscretizationPackage.Nunit + "  " + this.DiscretizationPackage.Fname + "\r\n";
            foreach (Package pack in this.Packages)
            {
                content += pack.FileTypeString + "  " + pack.Nunit + "  " + pack.Fname + "\r\n";
            }

            content += "LMT6  66  mt3dms.LMT" + "\r\n";
            foreach (Package pack in this.Packages)
            {
                if (pack.FileTypeString == "OC")
                {
                    POutputControl oc = (POutputControl)pack;


                    //content += "DATA" + "  " + oc.IHEDUN + "  " + "IHEDUN.dat" + "\r\n";
                    //content += "DATA" + "  " + oc.IDDNUN + "  " + "IDDNUN.dat" + "\r\n";
                    //content += "DATA" + "  " + oc.IBOUUN + "  " + "IBOUUN.dat" + "\r\n";


                    content += "DATA(BINARY)" + "  " + oc.IHEDUN + "  " + "IHEDUN.dat" + "\r\n";
                    content += "DATA(BINARY)" + "  " + oc.IDDNUN + "  " + "IDDNUN.dat" + "\r\n";
                    content += "DATA" + "  " + oc.IBOUUN + "  " + "IBOUUN.dat" + "\r\n";


                }

            }

            content += "DATA(BINARY)" + "  " + "101" + "  " + "ccf\\wel.bud" + "\r\n";  //井
            content += "DATA(BINARY)" + "  " + "102" + "  " + "ccf\\riv.bud" + "\r\n";  //河流
            content += "DATA(BINARY)" + "  " + "103" + "  " + "ccf\\drn.bud" + "\r\n";  //drain
            content += "DATA(BINARY)" + "  " + "104" + "  " + "ccf\\ghb.bud" + "\r\n";  //PGeneralHeadBoundary
            //content += "DATA(BINARY)" + "  " + "105" + "  " + "ccf\\chd.bud" + "\r\n";  //PSpecifiedHead
            content += "DATA(BINARY)" + "  " + "106" + "  " + "ccf\\rch.bud" + "\r\n";  //PRecharge
            content += "DATA(BINARY)" + "  " + "107" + "  " + "ccf\\evt.bud" + "\r\n";  //PEvapotranspiration
            content += "DATA(BINARY)" + "  " + "108" + "  " + "ccf\\lpf.bud" + "\r\n";  //PLayerPropertyFlow
            content += "DATA(BINARY)" + "  " + "109" + "  " + "ccf\\bc6.bud" + "\r\n";  //PBlockCenteredFlow

            HAUtil.FileOperations.WriteFile(FileNamePath, content);
        }

       /// <summary>
       /// 写*.lmt文件
       /// </summary>
       /// <param name="FileNamePath"></param>
        public void WriteLMTFile(string FileNamePath)
        {
            string str = "OUTPUT_FILE_NAME    mt3dms.ftl" + "\r\n";
            str += "OUTPUT_FILE_UNIT    333" + "\r\n";
            str += "OUTPUT_FILE_HEADER  extended" + "\r\n";

            //str += "OUTPUT_FILE_HEADER  sequential" + "\r\n";

            str += "OUTPUT_FILE_FORMAT  unformatted" + "\r\n";

            HAUtil.FileOperations.WriteFile(FileNamePath, str);
        }

        #region 对外主功能接口

        public void WriteFiles(string directory)
       {
           string name = directory + "solution.nam";

           this.WriteNamFile(name);


           this._basicPackage.WriteFile(directory);
           this._discretizationPackage.WriteFile(directory);

           foreach (Package pack in this._packages)
           {
               pack.WriteFile(directory);
           }

           string Path =directory+ "mt3dms.lmt";
           WriteLMTFile(Path);

       }


        static   public void DeleteModflowFiles(string directory)
        {
            try
            {
              
                //删除modflow文件 包括配置文件和生成的数据文件
                //solution.nam文件
                if (File.Exists(directory + "solution.nam"))
                {
                    File.Delete(directory + "solution.nam");
                }
                //solution.ba6
                if (File.Exists(directory + "solution.ba6"))
                {
                    File.Delete(directory + "solution.ba6");
                }
                //solution.dis
                if (File.Exists(directory + "solution.dis"))
                {
                    File.Delete(directory + "solution.dis");
                }
                //solution.lpf
                if (File.Exists(directory + "solution.lpf"))
                {
                    File.Delete(directory + "solution.lpf");
                }
                //solution.sip
                if (File.Exists(directory + "solution.sip"))
                {
                    File.Delete(directory + "solution.sip");
                }
                //solution.oc
                if (File.Exists(directory + "solution.oc"))
                {
                    File.Delete(directory + "solution.oc");
                }
                //mt3dms.lmt
                if (File.Exists(directory + "mt3dms.lmt"))
                {
                    File.Delete(directory + "mt3dms.lmt");
                }
                //mt3dms.ftl
                if (File.Exists(directory + "mt3dms.ftl"))
                {
                    File.Delete(directory + "mt3dms.ftl");
                }
                //solution.lst
                if (File.Exists(directory + "solution.lst"))
                {
                    File.Delete(directory + "solution.lst");
                }

                // "solution.mlt"
                if (File.Exists (directory + "solution.mlt"))
                {
                    File.Delete(directory + "solution.mlt");
                }






                //"solution.riv"
                if (File .Exists (directory +"solution.riv"))
                {
                    File.Delete(directory +"solution.riv");
                }

                //"solution.chd"
                if (File.Exists (directory +"solution.chd"))
                {
                    File.Delete(directory +"solution.chd");
                }

                //"solution.wel"
                if (File.Exists (directory +"solution.wel"))
                {
                    File.Delete(directory +"solution.wel");
                }



                //"solution.bc6"
                if (File.Exists (directory +"solution.bc6"))
                {
                    File.Delete(directory +"solution.bc6");
                }


                //"solution.hfb"
                if (File.Exists (directory +"solution.hfb"))
                {
                    File.Delete(directory +"solution.hfb");
                }
                //"solution.zne"
                if (File.Exists (directory +"solution.zne"))
                {
                    File.Delete(directory +"solution.zne");
                }
                //"solution.de4"
                if (File.Exists (directory +"solution.de4"))
                {
                    File.Delete(directory +"solution.de4");
                }
                //"solution.drn"
                if (File.Exists (directory + "solution.drn"))
                {
                    File.Delete(directory + "solution.drn");
                }
                //"solution.ghb"
                if (File.Exists(directory + "solution.ghb"))
                {
                    File.Delete(directory + "solution.ghb");
                }
                //"solution.pcg"
                if (File .Exists (directory +"solution.pcg"))
                {
                    File.Delete(directory +"solution.pcg");   
                }
                //"solution.rch"
                if (File .Exists (directory +"solution.rch"))
                {
                    File.Delete(directory +"solution.rch");
                }

                //solution.uzf
                if (File.Exists (directory +"solution.uzf"))
                {
                    File.Delete(directory +"solution.uzf");
                }

                //"solution.evt"
                if (File.Exists (directory +"solution.evt"))
                {
                    File.Delete(directory +"solution.evt");
                }



                //IBOUUN.dat   IDDNUN.dat   IHEDUN.dat

                if (File.Exists(directory + "IBOUUN.dat"))
                {
                    File.Delete(directory + "IBOUUN.dat");
                }
                if (File.Exists(directory + "IDDNUN.dat"))
                {
                    File.Delete(directory + "IDDNUN.dat");
                }
                if (File.Exists(directory + "IHEDUN.dat"))
                {
                    File.Delete(directory + "IHEDUN.dat");
                }
                //ccf
                if (Directory.Exists(directory + "ccf"))
                {
                    Directory.Delete(directory + "ccf", true);
                }

            }
            catch
            {
                return;
            }
        
        
        }







        #endregion


    }
}
